﻿using System;
using System.Xml;

namespace BuzzX8.Xmpp.DataForms
{
    public class Form : CompositeElement
    {
        public Form() : base("x")
        {}

        public string Title { get; set; }

        public string Instructions { get; set; }

        public FormType Type { get; set; }

        protected override void WriteStartElement(XmlWriter writer)
        {
            //writer.WriteStartElement(Name, Stanza.XDataNamespace);
            WriteType(writer);
        }

        private void WriteType(XmlWriter writer)
        {
            string typeStr = GetStringForType();
            writer.WriteAttributeString("type", typeStr);
        }

        private string GetStringForType()
        {
            switch (Type)
            {
                case FormType.Cancel:
                    return "cancel";

                case FormType.Form:
                    return "form";

                case FormType.Result:
                    return "result";

                case FormType.Submit:
                    return "submit";

                default:
                    throw new NotImplementedException();
            }
        }

        protected override void WritePayload(XmlWriter writer)
        {
            WriteTitle(writer);
            WriteInstructions(writer);
            base.WritePayload(writer);
        }

        private void WriteTitle(XmlWriter writer)
        {
            if(!string.IsNullOrEmpty(Title)) writer.WriteElementString("title", Title);
        }

        private void WriteInstructions(XmlWriter writer)
        {
            if(!string.IsNullOrEmpty(Instructions)) writer.WriteElementString("instructions", Instructions);
        }
    }
}
